import time
from Sentry3 import *
from mpython import *
from machine import UART


uart1 = UART(1, baudrate=57600, tx=Pin.P14, rx=Pin.P13)

Sentry3 = Sentry3(0x60)

brightness=9

# 自定义函数
def UART():
  global my_variable, i, Start, num
  Sentry3.begin(uart1)
def I2C():
  global my_variable, i, Start, num
  Sentry3.begin(i2c)
def Result_Display():
  global my_variable, i, Start, num
  # Sentry3不主动返回检测识别结果，需要主控板发送指令进行读取。读取的流程：首先读取识别结果的数量，接收到指令后，Sentry3会刷新结果数据，如果结果数量不为零，那么主控再发送指令读取结果的相关信息。请务必按此流程构建程序。
  num = (Sentry3.GetValue(sentry3_vision_e.kVisionYolo, sentry_obj_info_e.kStatus))
  i = 1
  for index in range(num):
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kAirplane):
      oled.DispChar("AirPlane", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kBicycle):
      oled.DispChar("Bicycle", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kBird):
      oled.DispChar("Bird", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kBoat):
      oled.DispChar("Boat", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kBottle):
      oled.DispChar("Bottle", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kBus):
      oled.DispChar("Bus", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kCar):
      oled.DispChar("Car", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kCat):
      oled.DispChar("Cat", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kChair):
      oled.DispChar("Chair", 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)==yolo_label_e.kCow):
      oled.DispChar("Cow", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 20):
      oled.DispChar("Tvmonitor", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 11):
      oled.DispChar("DiningTable", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 12):
      oled.DispChar("Dog", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 13):
      oled.DispChar("Horse", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 14):
      oled.DispChar("Motorbike", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 15):
      oled.DispChar("Person", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 16):
      oled.DispChar("PottedPlant", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 17):
      oled.DispChar("Sheep", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 18):
      oled.DispChar("Sofa", 0, (2-1)*16, 1)
    if ((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kLabel,i)) == 19):
      oled.DispChar("Train", 0, (2-1)*16, 1)
    oled.DispChar((str("x=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kXValue,i)))), 0, 32, 1)
    oled.DispChar((str("y=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kYValue,i)))), 64, 32, 1)
    oled.DispChar((str("w=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kWidthValue,i)))), 0, 48, 1)
    oled.DispChar((str("h=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionYolo,sentry_obj_info_e.kHeightValue,i)))), 64, 48, 1)
    oled.show()
    time.sleep(0.2)
    oled.fill_rect(0, (2-1)*16, 128, 16, 0)
    oled.fill_rect(0, (3-1)*16, 128, 16, 0)
    oled.fill_rect(0, (4-1)*16, 128, 16, 0)
    oled.show()
    i = (i + 1)


num = 0
i = 0
# 等待Sentry2完成初始化，此延时不可省略
time.sleep(2)
I2C()
Sentry3.VisionBegin(sentry3_vision_e.kVisionYolo)
oled.DispChar("Algo:20Class", 0, (1-1)*16, 1)
oled.show()
while True:
  rgb[1] = (0*brightness//9, 0*brightness//9, 255*brightness//9)
  rgb.write()
  Result_Display()
  rgb[1] = (0*brightness//9, 0*brightness//9, 0*brightness//9)
  rgb.write()
  time.sleep(0.2)
